/**
 * 文件名：TokenMangerController.java
 *
 * 创建人：虞云波 - .com
 *
 * 创建时间：2017年10月13日 上午10:34:38
 *
 * 版权所有：江苏晟邦网络科技有限公司
 */
package com.cbwl.eoas.web.controller.appint;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cbwl.eoas.cms.api.sysconfig.ISysUserService;
import com.cbwl.eoas.cms.model.sysconfig.SysUserDTO;
import com.cbwl.eoas.common.cache.redis.RedisCache;
import com.cbwl.eoas.common.cache.redis.RedisKeyPrefix;
import com.cbwl.eoas.common.util.UuidGenerator;
import com.cbwl.eoas.common.web.Result;
import com.cbwl.eoas.web.framework.annotation.InterceptorUrl;

/**
 * <p>[描述信息：toke管理Controller]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月13日 上午10:34:38
 */
@RestController
@RequestMapping("/v1/token")
public class TokenMangerController {

	
	/**
	 * redis数据源注入
	 */
	@Autowired
	private RedisCache<String,String> redisCache;
	
	@Autowired
	private ISysUserService sysUserService;
	
	
	/**
	 * 
	 * <p>根据工号重新获取tokenId</p>
	 * 
	 * @param request
	 * @param bean
	 * @return
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年11月2日 上午10:05:06
	 */
	@RequestMapping(value="/getTokenId",method = RequestMethod.POST)
	@InterceptorUrl(validsecond=5,validnum=1)
	public Object getTokenId(HttpServletRequest request,SysUserDTO bean){
		
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		
		if(StringUtils.isEmpty(bean.getUserJobno())){
			result.setCode("CBWL-000010");
			return result;
		}
		//根据login_name查询出用户基本信息
		List<SysUserDTO> list = sysUserService.queryObjectList(bean);
		if(list !=null && list.size()>0){
			bean = list.get(0);
		}else{
			result.setCode("CBWL-000010");
			return result;
		}
		
		String tokenId =UuidGenerator.getUuid();
		//生成token
		//用户存活时间，单位小时
		int liveTime = 24*3;
		String time =redisCache.getStr(RedisKeyPrefix.PARAM_PREFIX+"key_tokenId_livetime");
		if(!StringUtils.isEmpty(time)){
			liveTime =Integer.valueOf(time);
		}
		redisCache.putStr(tokenId,String.valueOf(bean.getId()),3600*liveTime);
		
		Map<String,String> map= new HashMap<>();
		map.put("tokenId",tokenId);
		map.put("expiresIn",String.valueOf(3600*liveTime));
		result.setData(map);
		
		return result;
	}
	
	
}
